package com.yunhai.device.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunhai.common.dto.GlobalException;
import com.yunhai.common.dto.PaginationVo;
import com.yunhai.common.utils.ObjectUtil;
import com.yunhai.common.utils.ReflectionCopy;
import com.yunhai.device.mapper.*;
import com.yunhai.device.service.DeviceInfoService;
import com.yunhai.model.entity.dms.DmsDeviceCategoryEntity;
import com.yunhai.model.entity.dms.DmsDeviceInfoEntity;
import com.yunhai.model.entity.fms.FmsCodeDictEntity;
import com.yunhai.model.entity.tms.TmsStoreEntity;
import com.yunhai.model.vo.dms.DmsDeviceInfoQuery;
import com.yunhai.model.vo.dms.DmsDeviceInfoVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class DeviceInfoServiceImpl implements DeviceInfoService {

    @Resource
    private DeviceInfoMapper deviceInfoMapper;
    @Resource
    private DeviceCategoryMapper deviceCategoryMapper;
    @Resource
    private FmsCodeDictMapper fmsCodeDictMapper;
    @Resource
    private TmsStoreMapper storeMapper;
    @Resource
    private TmsStoreGroupMapper storeGroupMapper;
    /**
     * 获取 DeviceInfo 分页列表
     */
    @Transactional
    public IPage getDeviceInfoPagination(PaginationVo paginationVo, DmsDeviceInfoQuery dmsDeviceInfoQuery ) throws Exception {

        IPage page = new Page( paginationVo.getPageIndex(), paginationVo.getPageSize() );
        LambdaQueryWrapper<DmsDeviceInfoEntity> lqs = new LambdaQueryWrapper();
        lqs.likeRight(dmsDeviceInfoQuery.getName() != null,
                DmsDeviceInfoEntity::getName,
                dmsDeviceInfoQuery.getName() );
        lqs.likeRight(dmsDeviceInfoQuery.getSn() != null,
                DmsDeviceInfoEntity::getSn,
                dmsDeviceInfoQuery.getSn() );
        lqs.eq(dmsDeviceInfoQuery.getCategoryId() != null,
                DmsDeviceInfoEntity::getCategoryId,
                dmsDeviceInfoQuery.getCategoryId() );
        lqs.eq(dmsDeviceInfoQuery.getStoreId() != null,
                DmsDeviceInfoEntity::getStoreId,
                dmsDeviceInfoQuery.getStoreId() );
        lqs.eq(dmsDeviceInfoQuery.getStatus() != null,
                DmsDeviceInfoEntity::getStatus,
                dmsDeviceInfoQuery.getStatus() );
        lqs.orderByDesc(DmsDeviceInfoEntity::getCreateTime);
        deviceInfoMapper.selectPage(page, lqs);

        List<DmsDeviceInfoEntity> record = page.getRecords();
        if( !ObjectUtil.isNotEmptyList(record)){
            return page;
        }

        /**
         * 获取分类
         * */
        Set<Long> categoryIds = record.stream().map(item->item.getCategoryId()).collect(Collectors.toSet());
        if( ObjectUtil.isNotEmptyList(categoryIds) ){
            List<DmsDeviceCategoryEntity> categoryList = deviceCategoryMapper.selectBatchIds(categoryIds);
            if( ObjectUtil.isNotEmptyList(categoryList) ){
                for( DmsDeviceInfoEntity item : record ){
                    categoryList.stream().filter(row->{
                        return row.getId().equals(item.getCategoryId());
                    }).findFirst().ifPresent(row->{
                        item.setCategoryName(row.getName());
                        item.setProductUrl(row.getUrl());
                    });
                }
            }
        }

        /**
         * 获取门店
         * */
        Set<Long > storeIds = record.stream().map(item->item.getStoreId()).collect(Collectors.toSet());
        if( ObjectUtil.isNotEmptyList(storeIds) ) {
            List<TmsStoreEntity> storeList = storeMapper.selectBatchIds(storeIds);
            if( ObjectUtil.isNotEmptyList(storeList) ) {
                for( DmsDeviceInfoEntity item : record ){
                    storeList.stream().filter(row->{
                        return row.getId().equals(item.getStoreId());
                    }).findFirst().ifPresent(row->{
                        item.setStoreName(row.getName());
                    });
                }
//              Set<Long> storeGroupIds = storeList.stream().map(item->item.getStoreGroupId()).collect(Collectors.toSet());
//              List<TmsStoreGroupEntity> tmsStoreGroupEntities = storeGroupMapper.selectBatchIds(storeGroupIds);
            }

        }

        /**
         * 获取区域
         * */

        return page;
    }


    /**
     * 获取 DeviceInfo 所有数据
     *
     */
    @Transactional
    public List<DmsDeviceInfoEntity> getDeviceInfoAllList( DmsDeviceInfoQuery dmsDeviceInfoQuery ) throws Exception {

        LambdaQueryWrapper<DmsDeviceInfoEntity> lqs = new LambdaQueryWrapper();
        lqs.eq(dmsDeviceInfoQuery.getStoreId() != null,
                DmsDeviceInfoEntity::getStoreId,
                dmsDeviceInfoQuery.getStoreId() );

        List<DmsDeviceInfoEntity> list = deviceInfoMapper.selectList(lqs);
        return list;
    }


    /**
     * 获取 DeviceInfo 详情
     */
    @Transactional
    public DmsDeviceInfoEntity getDeviceInfoDetail( Long id) throws Exception {

        DmsDeviceInfoEntity dmsDeviceInfoEntity = deviceInfoMapper.selectById(id);
        return dmsDeviceInfoEntity;
    }

    public DmsDeviceInfoEntity getDeviceInfoDetailByImei( String imei) throws Exception {

        LambdaQueryWrapper<DmsDeviceInfoEntity> lqs = new LambdaQueryWrapper<>();
        lqs.eq(DmsDeviceInfoEntity::getSn, imei);
        return deviceInfoMapper.selectOne( lqs );
    }

    /**
     * 新增 DeviceInfo
     * */
    @Transactional
    public void addDeviceInfo(DmsDeviceInfoVo dmsDeviceInfoVo) throws Exception {

        DmsDeviceInfoEntity dmsDeviceInfoEntity = new DmsDeviceInfoEntity();
        ReflectionCopy.copyProperties( dmsDeviceInfoEntity, dmsDeviceInfoVo );
        deviceInfoMapper.insert(dmsDeviceInfoEntity);

        FmsCodeDictEntity fmsCodeDictEntity = new FmsCodeDictEntity();
        fmsCodeDictEntity.setId(102L);
        fmsCodeDictEntity.setLastCode(dmsDeviceInfoVo.getNumber());
        fmsCodeDictMapper.updateById(fmsCodeDictEntity);
        return;
    }


    /**
     * 更新 DeviceInfo
     * */
    @Transactional
    public void updateDeviceInfo( DmsDeviceInfoVo dmsDeviceInfoVo) throws Exception {

        if( dmsDeviceInfoVo.getId() == null ){
            throw GlobalException.gen(10021, "id不存在");
        }
        DmsDeviceInfoEntity dmsDeviceInfoEntity = new DmsDeviceInfoEntity();
        ReflectionCopy.copyProperties( dmsDeviceInfoEntity, dmsDeviceInfoVo );
        deviceInfoMapper.updateById(dmsDeviceInfoEntity);
        return;
    }

    /**
     * 删除 DeviceInfo
     * */
    @Transactional
    public void batchRemoveDeviceInfo( List<Long> ids ) throws Exception {

        if( !ObjectUtil.isNotEmptyList(ids) ){
            throw GlobalException.gen(10011, "要删除的id不存在");
        }
        deviceInfoMapper.deleteBatchIds(ids);
    }

    @Override
    public List<DmsDeviceInfoEntity> getDeviceInfoListByIds(Set<Long> deviceIds) throws Exception {
        return  deviceInfoMapper.selectBatchIds(deviceIds);
    }

}